const App = getApp()
import mode from '../../utils/mode'
const wxParse = require('../../wxParse/wxParse.js');
import {
  setStore,
  getStore
} from '../../utils/storage'
import {
  getJoinUsInfo,
  login
} from '../../api/user/user'

import {
  showTan
} from '../../utils/util'

Page({
  onShareAppMessage () {   //分享好友
    // this.setData({
    //   isShare:1,
    // })
    return {
      title:this.data.shareText||'2AM官方小程序',
      path: `/pages/shareIndex/shareIndex?share_id=${this.data.userInfo&&this.data.userInfo.id?this.data.userInfo.id:'0'}&userName=${this.data.userInfo&&this.data.userInfo.user_nickname?this.data.userInfo.user_nickname:''}`,
      imageUrl: 'https://2am-project.cn/img/20220325/shareImg.png',   //分享图片
    }
  },
  onShareTimeline () {  //分享朋友圈
    return {
      title: '2AM官方小程序',
      query: 'share_id=' + this.data.userInfo.id,
      // imageUrl:'',   //分享图片
    }
  },
  data: {
    temIds:['Wzq0Q8OpleP_KgHNxStTKJ6-8NdvRvqgJnioZ01w7bg','IKdsAyES45qURTGEQMaHqLRq-fpoaU83kNRtz3TcC78'],
    isShare:0,  //是否进行分享
    showShare:false,  //分享成功展示
    isOk:false,
    aboutInfo:null,
    shareText: '2AM官方小程序',  //分享内容
    expiredCount: '0',
    num_num: '0',
    share_id: '',
    shareOptions: [
      // { name: '朋友圈', icon: 'wechat-moments', openType: 'share' },
      { name: '微信', icon: 'wechat', openType: 'share' },
      // { name: '复制链接', icon: 'link' },
      // { name: '分享海报', icon: 'poster' },
      // { name: '二维码', icon: 'qrcode' },
    ],



    info: '',
    whiteday: '../../image/logoNight@2x.png',
    logo: App.globalData.logo,
    logoStyle: 'width: 221px; height: 74px',
    username: '',
    curTime: 'Good Morning',
    swiperGoodsIdx: 0,
    swiperCaseIdx: 0,
    night: false,
    testing: false,
    booking: false,
    isMember: false,
    score_info: 0,

    desc: '1+29，医美+护肤是2AM推崇的科学护理搭配。',
    dec2: '选择适合当下皮肤状态的护肤产品，才是精准有效的 护肤之道。',

    testingImage: '../../image/indexTestImg.png',
    goodsImages: [],
    caseImages: [],  //案例轮播图片
    caseList: [   //案例列表
      {
        title: '案例1：28岁 换季干敏，衰老胯脸初现痕迹',
        case: '#翻车急救组合',
        type: 'HYPER HEAL PRESCRIPTION Ⅰ型',
        status: 'SKIN CONCERN 皮肤状况',
        list: ['医美术后返干易敏', '口罩脸发痒泛红', '干燥起皮'],
        subTitle: "CONTENTS 护理包",
        proList: [
          {
            night: 'https://2am-project.cn/img/20220325/Day/A1.png',
            day: 'https://2am-project.cn/img/20220325/Night/A1_night.png',
            name: 'HYPER FILLER AMPOULE',
            spec: '嘭盈精华液',
            color: 'rgb(242,219,133)'
          },
          {
            night: 'https://2am-project.cn/img/20220325/Day/A2.png',
            day: 'https://2am-project.cn/img/20220325/Night/A2_night.png',
            name: 'SUPER HEAL AMPOULE',
            spec: '舒颜精华液',
            color: 'rgb(148,189,146)'
          },
          {
            night: 'https://2am-project.cn/img/20220325/Day/A3.png',
            day: 'https://2am-project.cn/img/20220325/Night/A3_night.png',
            name: 'ANTI-AGING AMPOULE',
            spec: '焕活精华液',
            color: 'rgb(205,121,69)'
          },
        ]
      },
      {
        title: '案例2：25岁 熬夜加班狂，嗜糖如命',
        case: '#熬夜闪充组合',
        type: 'ANTI-OXIDANT PRESCRIPTION Ⅰ型',
        status: 'SKIN CONCERN 皮肤状况',
        list: ['下垂松垮，满脸倦容', '脸黄暗沉', '皮肤粗糙','肤色不均'],
        subTitle: "CONTENTS 护理包",
        proList: [
          {
            night: 'https://2am-project.cn/img/20220325/Day/A1.png',
            day: 'https://2am-project.cn/img/20220325/Night/A1_night.png',
            name: 'HYPER FILLER AMPOULE',
            spec: '嘭盈精华液',
            color: 'rgb(242,219,133)'
          },
          {
            night: 'https://2am-project.cn/img/20220325/Day/A3.png',
            day: 'https://2am-project.cn/img/20220325/Night/A3_night.png',
            name: 'ANTI-AGING AMPOULE',
            spec: '焕活精华液',
            color: 'rgb(205,121,69)'
          },
          {
            night: 'https://2am-project.cn/img/20220325/Day/A5.png',
            day: 'https://2am-project.cn/img/20220325/Night/A5_night.png',
            name: 'BRIGHT BOOST AMPOULE',
            spec: '焕颜精华液',
            color: 'rgb(197,225,232)'
          },
        ]
      },
      {
        title: '案例3：21岁 常年油光满面，油炸火锅爱好者',
        case: '#油田救星组合',
        type: 'RENEW OILY SKIN PRESCRIPTION Ⅱ型',
        status: 'SKIN CONCERN 皮肤状况',
        list: ['皮脂分泌旺盛', '皮肤暗黄', '外油内干','毛孔粗大'],
        subTitle: "CONTENTS 护理包",
        proList: [
          {
            night: 'https://2am-project.cn/img/20220325/Day/A2.png',
            day: 'https://2am-project.cn/img/20220325/Night/A2_night.png',
            name: 'SUPER HEAL AMPOULE',
            spec: '舒颜精华液',
            color: 'rgb(148,189,146)'
          },
          {
            night: 'https://2am-project.cn/img/20220325/Day/A3.png',
            day: 'https://2am-project.cn/img/20220325/Night/A3_night.png',
            name: 'ANTI-AGING AMPOULE',
            spec: '焕活精华液',
            color: 'rgb(205,121,69)'
          },
          {
            night: 'https://2am-project.cn/img/20220325/Day/A4.png',
            day: 'https://2am-project.cn/img/20220325/Night/A4_night.png',
            name: 'SEBUM CONTROL AMPOULE',
            spec: '清透精华液',
            color: 'rgb(137,65,43)'
          },
        ]
      },
      {
        title: '案例4：20岁 一言不合就爆痘，色素易沉体质',
        case: '#痘印橡皮擦组合',
        type: 'REFRESHING PRESCRIPTION Ⅱ型',
        status: 'SKIN CONCERN 皮肤状况',
        list: ['新鲜红痘印', '色沉斑点杂质', '陈年痘坑','黑色素沉着'],
        subTitle: "CONTENTS 护理包",
        proList: [
          {
            night: 'https://2am-project.cn/img/20220325/Day/A1.png',
            day: 'https://2am-project.cn/img/20220325/Night/A1_night.png',
            name: 'HYPER FILLER AMPOULE',
            spec: '嘭盈精华液',
            color: 'rgb(242,219,133)'
          },
          {
            night: 'https://2am-project.cn/img/20220325/Day/A2.png',
            day: 'https://2am-project.cn/img/20220325/Night/A2_night.png',
            name: 'SUPER HEAL AMPOULE',
            spec: '舒颜精华液',
            color: 'rgb(148,189,146)'
          },
          {
            night: 'https://2am-project.cn/img/20220325/Day/A5.png',
            day: 'https://2am-project.cn/img/20220325/Night/A5_night.png',
            name: 'BRIGHT BOOST AMPOULE',
            spec: '焕颜精华液',
            color: 'rgb(197,225,232)'
          },
        ]
      },
    ],

    have: {
      num: 0,
      title: '我的优惠券',
      desc: '0张即将到期赶紧点击去使用',
      url: '/pages/coupons/coupons'
    },

  },
  onLoad (options) {
    var info = wx.getStorageSync('uinfo')
    wx.setStorage({ key: 'isFrom', data: '' })
    if (options && options.share_id) {
      wx.setStorage({ key: "share_id", data: options.share_id })

      setStore('share_id', options.share_id)
    }
  },
  setInfo(){// 订阅消息
    let that = this
    this.setData({
      showShare:false,
      isShare:0
    })
    let shareData={
      user_id:that.data.userInfo.id,
      type:3,
      other_type:'',
    }
    wx.requestSubscribeMessage({
        tmplIds: that.data.temIds,
        success (res) {
          let arr = []
            if(res[that.data.temIds[0]]=='accept'){//拒绝
              arr.push('1')
            }
            if(res[that.data.temIds[1]]=='accept'){
              arr.push('2')
            }
            // else if(res[that.data.temIds[2]]=='accept'){
              // arr.push('3')
            // }else if(res[that.data.temIds[3]]=='accept'){  //允许
              // arr.push('4')
            // }
            if(arr.length>0){
              shareData.other_type = arr.join(',')
              that.sendSetYue(shareData)
            }
        },
        fail(error){
          console.log(error,'123失败');
        }
      })
  },
  sendSetYue(data){
    wx.$api.sendTemplate(data).then(res=>{
            if(res.code>0){
            }else{
                wx.showToast({
                  title: res.msg,
                  icon:'none'
                })
        }
    })
},
  onHide () {
    wx.setStorage({ key: 'isFrom', data: '/pages/index/index' })
  },
  onShow () {
    var info = wx.getStorageSync('uinfo')
    if (info && info.user) {
      this.setData({
        user_id: info.user.id
      })
      this.getUser()
      let n = info.user.user_nickname || ''
      let title =  n + '邀你在线肌肤问诊，点...'
      this.setData({
        shareText: title,
        info: info,
        userInfo:info.user,
        username: info.user.user_nickname,
        isMember: true
      })
      wx.showShareMenu({
        menus: ['shareAppMessage']
      })
    }else{
      wx.hideShareMenu({
        menus: ['shareAppMessage', 'shareTimeline']
      })
    }
    this.takeBanner()
    // if(this.data.isShare>0){
    //   this.setData({
    //     showShare:true
    //   })
    // }
    this.notifyTime(new Date().getHours())
    this.setTabbar()

    this.getAboutUs()
    this.getCouponNum()
    this.getIntegralGoods()
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
    this.setData({
      isOk:false
    })
  },
  toGoods(e){  //查看商品详情
    let index = e.currentTarget.dataset.index
    // 28  29  30   33
    if(index==0){
      wx.navigateTo({
        url: '/pages/product-details/product-details?goodId=28'
      })
    }else if(index==1){
      wx.navigateTo({
        url: '/pages/product-details/product-details?goodId=29'
      })
    }else if(index==2){
      wx.navigateTo({
        url: '/pages/product-details/product-details?goodId=30'
      })
    }else if(index==3){
      wx.navigateTo({
        url: '/pages/product-details/product-details?goodId=33'
      })
    }
  },
  addKF(){
    wx.navigateTo({
      url: '/pages/servers-card/servers-card',
    })
  },
  okk(){
    this.setData({
      isOk:true
    })
  },
  goCar () {
    wx.switchTab({
      url: '/pages/exclusive/exclusive',
    })
  },
  getAboutUs(){
    var that = this;
    wx.$api.getAboutUsInfo().then(res=>{
        var parseContent = wxParse.wxParse('article', 'html', res.data.article_content, that, 5)
          that.setData({
              aboutInfo:res.data
          })
    })
},
  goGoodInfo (e) {
    let item = e.currentTarget.dataset.item
    if (!this.data.user_id && item.banner_type<1) {
      wx.navigateTo({
        url: '/pages/register/register',
      })
    }else if(item.banner_activity_img_url){
      wx.navigateTo({
        url: '/pages/activePage/activePage?name='+item.banner_title+'&url='+item.banner_activity_img_url,
      })
    } else if(item.banner_type<1){
      wx.navigateTo({
        url: '/pages/goods-details/goods-details?is_test=1&goodId=' + item.goods_ids[0]
      })
    }else if (item.banner_contact_type_name == '商品') {
      if (item.goods_ids.length > 1) {
        let ids = item.id
        wx.navigateTo({
          url: '/pages/goodList/goodList?isBanner=1&bannerId=' + ids
        })
      } else {
        wx.navigateTo({
          url: '/pages/product-details/product-details?goodId=' + item.goods_ids[0]
        })
      }

    }
  },
  handleClickTesting () {
    var that = this
    var uinfo = that.data.userInfo
    if (!uinfo || uinfo.is_member < 1) {
      wx.setStorage({
        data: '/pages/questionnaire/questionnaire',
        key: 'lastUrl',
      })
      wx.navigateTo({
        url: '/pages/register/register',
      })
    } else {
      wx.navigateTo({
        url: '../questionnaire/questionnaire',
      })
    }
  },
  handleClickGoods () {
    // if (checkLogin()) {
    //   console.log('加购')
    // }
  },
  handleClickShare () {
    if (this.data.userInfo && this.data.userInfo.id) {
      wx.navigateTo({
        url: '/pages/sharePage/sharePage',
      })
    } else {
      wx.navigateTo({
        url: '/pages/register/register',
      })
    }
  },
  goRegister () {
    wx.navigateTo({
      url: '/pages/register/register',
    })
  },
  handleSwiperChange (e) {
    this.setData({
      swiperGoodsIdx: e.detail.current
    })
  },
  handleSwiperCaseChange (e) {
    this.setData({
      swiperCaseIdx: e.detail.current
    })
  },
  handleClick () {
    if (checkLogin()) {
      console.log('添加成功！可在购物车查看')
    }
  },
  onScroll (e) {
    const {
      scrollTop,
      deltaY
    } = e.detail
    if (scrollTop > 100 || scrollTop <= 0) return

    if (deltaY < 0) {
      this.transition(0)
    } else {
      this.transition(220)
    }
  },
  goInfo () {

  },
  handleClick () {
    // wx.navigateTo({ url: "/pages/coupons-details/coupons-details?id=12" })
  },
  transition (deltaY) {
    const offset = Math.min(Math.max(Math.abs(deltaY), 96), 220)
    this.setData({
      logoStyle: `width: ${offset}px; height: ${offset / 3}px;`
    })
  },
  notifyTime: function (hour) {
    var curTime = hour <= 11 ?
      'Good Morning' :
      hour <= 13 ?
        'Good Afternoom' :
        hour < 18 ?
          'Good Afternoon' :
          'Good Evening'
    this.setData({
      curTime: curTime
    })
  },
  
  setTabbar(){
    let night = this.hasNight(new Date().getHours())
    this.setData({
      night: night,
    })
    if (night) {
      wx.setTabBarStyle({
        color: '#AAA',
        selectedColor: '#fff',
        backgroundColor: '#000',
        borderStyle: "black",
      })
      wx.setTabBarItem({
        index: 0,
        selectedIconPath: "/image/navbar/1-1.png",
      }
      )
      wx.setTabBarItem({
        index: 1,
        selectedIconPath: "/image/navbar/2-1.png",
      })
      wx.setTabBarItem({
        index: 2,
        selectedIconPath: "/image/navbar/3-1.png",
      })
      wx.setTabBarItem({
        index: 3,
        selectedIconPath: "/image/navbar/4-1.png",
      })
    } else {
      wx.setTabBarStyle({
        color: '#AAA',
        selectedColor: '#000',
        backgroundColor: '#fff',
        borderStyle: "white",
      })
      wx.setTabBarItem({
        index: 0,
        selectedIconPath: "/image/navbar/1-3.png"
      }
      )
      wx.setTabBarItem({
        index: 1,
        selectedIconPath: "/image/navbar/2-3.png"
      })
      wx.setTabBarItem({
        index: 2,
        selectedIconPath: "/image/navbar/3-3.png"
      })
      wx.setTabBarItem({
        index: 3,
        selectedIconPath: "/image/navbar/4-3.png"
      })
    }
  },
  hasNight: function (hour) {
    return hour > 18 || hour < 7
  },
  takeBanner(){
    var that =this
    // that.setData({
    //   goodsImages: [],
    //   swiperGoodsIdx: 0
    // })
    let id = this.data.user_id||'0'
    wx.$api.getBannerListInfo({ user_id:id  }).then(async resData => {
        if(resData.code>0){
          that.setData({
            goodsImages: resData.data||[]
          })
        }else{
          wx.showToast({
            title: resData.msg,
            icon:'none'
          })
        }
    })
  },
  getUser () {
    var that = this
    wx.$api.getUserInfo({
        userid: that.data.user_id
      }).then(res=>{
        let n= res.data.user_nickname || '好友'
        let title =  n+ '邀你在线肌肤问诊，点...'
        that.setData({
          shareText: title,
          userInfo: res.data
        })
    })
  },
  toIntegral () {
    wx.navigateTo({
      url: '../integral-goods/integral-goods',
    })
  },
  getCouponNum () {
    var that = this
    wx.$api.getCouponNum({
        userid: that.data.user_id
      }).then(res=>{
        if(res.code){
          that.setData({
            num_num: res.data.count,
            expiredCount: res.data.expiredCount,
          })
      }
    })
  },
  toCoupon () {
    if(this.data.num_num<1){
      wx.navigateTo({
        url: '/pages/servers-card/servers-card?isCoupon=1',
      })
    }else{
      wx.navigateTo({
        url: '../coupons/coupons',
      })
    }
  },
  getIntegralGoods () {
    let that = this
    mode.getIntegralGoods().then(item => {
      that.setData({
        integralList: item.data
      })
    })
  },
})